/**
 *
 * @param {*} date
 * @param {*} format
 * @returns
 * @description 格式化时间
 * @example
 * formatTime(new Date(), 'yyyy-MM-dd w') // 输出类似 "2023-04-05 星期三"
 */
export function formatTime(date, format = 'yyyy-MM-dd HH:mm:ss') {
  console.log(date, format)

  date = new Date(date)
  const replacements = {
    yyyy: date.getFullYear(),
    MM: (date.getMonth() + 1).toString().padStart(2, '0'),
    dd: date.getDate().toString().padStart(2, '0'),
    HH: date.getHours().toString().padStart(2, '0'),
    mm: date.getMinutes().toString().padStart(2, '0'),
    ss: date.getSeconds().toString().padStart(2, '0'),
    w: '星期' + ['日', '一', '二', '三', '四', '五', '六'][getWeekday(date)],
  }
  function getWeekday(date) {
    return date.getDay().toString()
  }
  return format.replace(/(yyyy|MM|dd|HH|mm|ss|w)/g, (matched) => replacements[matched])
}

/**
 *
 * @param {*} obj {}
 * @param {*} tipsList [{ name: '对象名称', tips: '未填写时提示信息'}]
 * @returns
 * @description 表单验证
 * @example
 * notFilled({ name: '', age: 18 }, [{ name: 'name', tips: '姓名未填写' }])
 * // 输出：姓名未填写
 * 身份证：/^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
 * 手机号：/^((13[0-9])|(14[0-9])|(15[0-9])|(16[2-7])|(17[0-8])|(18[0-9])|(19[0-9]))\d{8}$/
 */
export function notFilled1(obj, tipsList) {
  let err = null
  for (const key of Object.keys(obj)) {
    for (const tip of tipsList) {
      if (tip.name == key && !obj[key]) {
        err = tip.tips
        break
      }
      if (tip.name == key && tip.reg && !tip.reg.test(obj[key])) {
        err = tip.tips1
        break
      }
      if (tip.name == key && tip.callback) {
        if (!tip.callback(obj[key])) {
          err = tip.tips
        }
      }
    }
  }
  if (err) {
    return uni.showToast({
      title: err,
      icon: 'none',
    })
  }
  return true
}
